Image Processing Functions

This module contains functions for processing images, including extracting patches, adding synthetic noise, and normalizing image stacks.

Usage Examples

Extracting Patches

from image_processing import extract_patches
import numpy as np

data = np.random.rand(100, 64, 64)  # Example dataset of 100 images, each 64x64
patches = extract_patches(data, num_patches=500, patch_size=16, strategy='random')

Adding Noise to Images

from image_processing import add_noise
import numpy as np

images = np.random.rand(100, 64, 64)  # Example dataset of 100 images, each 64x64
noisy_images = add_noise(images, gaussian_sigma=0.1)

Normalizing an Image Stack

from image_processing import normalize_image_stack
import numpy as np

stack = np.random.rand(100, 64, 64)  # Example image stack
normalized_stack = normalize_image_stack(stack)

Functions

encoding_information.image_utils._extract_random_patches(data, num_patches, patch_size, verbose)

Extract random patches from a dataset, used by both random and uniform_random strategies.

Parameters:
  • data (ndarray) – Input dataset to extract patches from.

  • num_patches (int) – The number of patches to extract.

  • patch_size (int) – Size of each patch.

  • verbose (bool) – If True, show progress with tqdm.

Returns:

Array of extracted patches.

Return type:

ndarray

encoding_information.image_utils.extract_patches(data, num_patches=1000, patch_size=16, strategy='random', crop_location=None, num_masked_pixels=256, seed=None, verbose=False) Array

Extract patches from a dataset using various strategies.

Parameters:
  • data (ndarray) – Input data from which to extract patches. Can have shapes (N, W, H), (N, W, H, C), or (N, D).

  • num_patches (int, optional) – Number of patches to extract.

  • patch_size (int, optional) – Size of the square patches.

  • strategy (str, optional) – Strategy for patch extraction (‘random’, ‘uniform_random’, ‘tiled’, ‘cropped’, ‘masked’).

  • crop_location (tuple, optional) – Top-left corner of the patch for ‘cropped’ strategy. If None, a random location is chosen.

  • num_masked_pixels (int, optional) – Number of pixels to mask in the ‘masked’ strategy.

  • seed (int, optional) – Random seed for reproducibility.

  • verbose (bool, optional) – If True, show progress during patch extraction.

Returns:

Extracted patches.

Return type:

ndarray

encoding_information.image_utils.add_noise(images, ensure_positive=True, gaussian_sigma=None, key=None, seed=None, batch_size=1000)

Add Poisson or Gaussian noise to a stack of images.

Parameters:
  • images (ndarray) – A stack of images (NxHxW) or patches (Nx(num pixels)).

  • ensure_positive (bool, optional) – Whether to ensure all resulting pixel values are non-negative.

  • gaussian_sigma (float, optional) – Standard deviation for Gaussian noise. If None, Poisson noise is added.

  • key (jax.random.PRNGKey, optional) – PRNGKey for generating noise. If None, a key is generated based on the seed.

  • seed (int, optional) – Seed for generating noise, if no key is provided.

  • batch_size (int, optional) – Number of images to process in batches.

Returns:

Noisy images.

Return type:

ndarray

encoding_information.image_utils.normalize_image_stack(stack)

Rescale pixel values to normalize the average energy across images.

Parameters:

stack (ndarray) – Stack of images to normalize.

Returns:

Normalized image stack.

Return type:

ndarray